package com.mwh.yuantu.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mwh.yuantu.entity.CollectionInfoEntity;
import com.mwh.yuantu.entity.VideoInfoEntity;
import com.mwh.yuantu.parameter.CollectionInfoParameter;
import com.mwh.yuantu.parameter.CollectionInfoSearchParameter;
import com.mwh.yuantu.dto.CollectionInfoDTO;
import org.springframework.beans.BeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * (CollectionInfo)表服务接口
 *
 * @author maweihong
 * @since 2023-11-01 09:47:29
 */
@Service
@Slf4j
public class CollectionInfoService extends BaseService {

    /**
     * 收藏或删除指定视频
     * @param collectionInfo
     */
    public void save(CollectionInfoEntity collectionInfo) {
        // 判断该视频是否已收藏过了
        int count = iCollectionInfoDAO.countByMediaIdAndUserId(collectionInfo.getMediaId()+"", collectionInfo.getUserId()+"");
        if (count==0) {
            // 添加收藏
            iCollectionInfoDAO.insert(collectionInfo);
        } else {
            // 删除收藏
            iCollectionInfoDAO.delete(collectionInfo);
        }
    }

    /**
     * 获取用户的视频收藏列表
     * @param map 用户id
     * @return
     */
    public List<Map<String, Object>> listCollection(Map<String , Object> map) {
        List<Map<String, Object>> list = iCollectionInfoDAO.listCollection(map);
        if (list!=null && list.isEmpty()==false) {
            // 查询最新一集
            int len = list.size();
            for (int i=0; i<len; i++) {
                String mediaId = list.get(i).get("media_id").toString();
                VideoInfoEntity videoInfo = iVideoInfoDAO.selectByMediaIdLast(mediaId);
                list.get(i).put("videoInfo", videoInfo);
            }
        }
        return list;
    }

    /**
     * 判断当前视频是否已被用户收藏过了
     * @param mediaId 媒体主键
     * @param userId 用户id
     * @return
     */
    public int countByMediaIdAndUserId(String mediaId, String userId) {
        return iCollectionInfoDAO.countByMediaIdAndUserId(mediaId, userId);
    }

    /**
     * 删除收藏的视频
     * @param collectionInfo
     */
    public void delete(CollectionInfoEntity collectionInfo) {
        iCollectionInfoDAO.delete(collectionInfo);
    }

}

